package com.example.aijie.autoreadhelper.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.aijie.autoreadhelper.R;
import com.example.aijie.autoreadhelper.appbean.BaseAppBean;

import java.text.MessageFormat;
import java.util.List;

/**
 * app 应用设置
 */
public class SetAdapter extends BaseAdapter {
    private List<BaseAppBean> mDatas;
    private Context context;

    public SetAdapter(List<BaseAppBean> mDatas, Context context) {
        this.mDatas = mDatas;
        this.context = context;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final BaseAppBean baseAppBean = mDatas.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_app_set, null);
            holder = new ViewHolder();
            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkBox);
            convertView.setTag(holder);
            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    baseAppBean.setEnableToday(isChecked);
                    baseAppBean.update(baseAppBean.getAppId());
                }
            });
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_name.setText(MessageFormat.format("名称：{0}", baseAppBean.getAppName()));
        holder.checkBox.setChecked(baseAppBean.isEnable());
        return convertView;
    }

    public class ViewHolder {
        TextView tv_name;
        CheckBox checkBox;
    }
}
